﻿package com
{
	import flash.events.KeyboardEvent;
	import flash.media.Sound;
	import fl.transitions.Tween;
	import fl.transitions.easing.*;
	import fl.transitions.TweenEvent;
	import flash.display.Sprite;
	import flash.text.TextField;
	import flash.text.TextFormat;
	import flash.text.TextFieldAutoSize;
	import flash.text.AntiAliasType;
	import flash.text.GridFitType;
	import flash.display.MovieClip;
	import flash.events.MouseEvent;
	import com.greensock.*;
	import com.greensock.easing.*;
	import com.*;
	
	public class NodeManager 
	{
		
		public var TxtFormatMgr:TextFormatManager = new TextFormatManager();
		public var RedTeamNodesList:Vector.<MovieClip> = new Vector.<MovieClip>;
		public var BlueTeamNodesList:Vector.<MovieClip> = new Vector.<MovieClip>;
		public var WhiteTeamNodesList:Vector.<MovieClip> = new Vector.<MovieClip>;
		
		public var knw:Vector.<MovieClip> = new Vector.<MovieClip>;
		public var knr:Vector.<MovieClip> = new Vector.<MovieClip>;
		public var knb:Vector.<MovieClip> = new Vector.<MovieClip>;
		

		var NodesToRemove:Vector.<MovieClip> = new Vector.<MovieClip>;

		
		var myScreen;
		
		public function NodeManager(input_screen)
		{
			myScreen = input_screen;
		}
		public function nextNode():void
		{
			
		}
		
		public function addNode(ip:String,color:String,_server:Boolean,OSType)
		{
			//Graphic For TextBlock
			var newNode:MovieClip = new MovieClip();
			var newNodeBG:MovieClip = new MovieClip();
			if(color=="red")
			{
				if(_server)
					newNodeBG = new IPBlockRed();
//				else if(_koth)
		//			newNodeBG = new IPBlockRedMini();
				else
					newNodeBG = new IPBlockRedMini();
			}
			else if(color=="blue")
			{
				if(_server)
					newNodeBG = new IPBlockBlue();
				//else if(_koth)
					//newNodeBG = new IPBlockBlueMini();
				else
					newNodeBG = new IPBlockBlueMini();
			}
			else if(color=="white")
			{
				if(_server)
					newNodeBG = new IPBlockWhite();
				//else if(_koth)
					//newNodeBG = new IPBlockWhiteMini();
				else
					newNodeBG = new IPBlockWhiteMini();
			}
			else
				return;
			
			newNodeBG.name="BG";
			newNode.addChild(newNodeBG);
			
			newNode.server=_server;
			
			//KOTH 
//			newNode.koth = _koth;
			
			newNode.numServices=0;
			
			if(_server)
			{
				var startingX=35;
				var startingY=5;
				var xDisplacement=14;
				var sshIcon:MovieClip = new Icon_SSH();
				sshIcon.alpha=.1
				sshIcon.name="ssh";
				sshIcon.y=startingY;
				sshIcon.x=startingX+(xDisplacement*0);
				var IconDown = new Icon_DOWN();
				IconDown.name="down";
				IconDown.alpha=0;
				sshIcon.addChild(IconDown);
				newNode.addChild(sshIcon);
				
				var ftpIcon:MovieClip = new Icon_FTP();
				ftpIcon.alpha=.1
				ftpIcon.name="ftp";
				ftpIcon.y=startingY;
				ftpIcon.x=startingX+(xDisplacement*1);
				var IconDown = new Icon_DOWN();
				IconDown.name="down";
				IconDown.alpha=0;
				sshIcon.addChild(IconDown);
				newNode.addChild(ftpIcon);
				
				var dbIcon:MovieClip = new Icon_DB();
				dbIcon.alpha=.1
				dbIcon.name="mysql";
				dbIcon.y=startingY;
				dbIcon.x=startingX+(xDisplacement*2);
				var IconDown = new Icon_DOWN();
				IconDown.name="down";
				IconDown.alpha=0;
				sshIcon.addChild(IconDown);
				newNode.addChild(dbIcon);
				
				var htmlIcon:MovieClip = new Icon_HTML();
				htmlIcon.alpha=.1
				htmlIcon.name="http";
				htmlIcon.y=startingY;
				htmlIcon.x=startingX+(xDisplacement*3);
				var IconDown = new Icon_DOWN();
				IconDown.name="down";
				IconDown.alpha=0;
				sshIcon.addChild(IconDown);
				newNode.addChild(htmlIcon);
			}
			
			/*else
			{
				    //trace(OSType);
					var icon_OS = new IconKOTH();
					icon_OS.name = "koth";
					icon_OS.x=4;
					icon_OS.y=5;
					newNode.addChild(icon_OS);
					newNode.osType = OSType; 			
			}
			*/else
			{
				trace(OSType);
				if(OSType=="windows")
				{
					var icon_OS = new Icon_WINDOWS();
					icon_OS.name = "os";
					icon_OS.x=4;
					icon_OS.y=5;
					newNode.addChild(icon_OS);
					newNode.osType =OSType; 
				}
				else if(OSType=="linux")
				{
					var icon_OS = new Icon_LINUX();
					icon_OS.name = "os";
					icon_OS.x=4;
					icon_OS.y=5;
					newNode.addChild(icon_OS);
					newNode.osType =OSType; 
				}
				else if (OSType=="KOTH")
				{
					var icon_OS = new Icon_Koth();
					icon_OS.name = "os";
					icon_OS.x=4;
					icon_OS.y=5;
					newNode.addChild(icon_OS);
					newNode.osType = OSType;
				}
				else
				{
					var icon_OS = new Icon_MAC();
					icon_OS.name = "os";
					icon_OS.x=4;
					icon_OS.y=5;
					newNode.addChild(icon_OS);
					newNode.osType =OSType;  
				}
			}
			
			var txtField=new TextField();
			if(_server)
				txtField.x=2;
			else
				txtField.x=20;
				
			txtField.y=2;
			txtField.text="";
			txtField.name="ip";
			txtField.text=ip;
			txtField.setTextFormat(TxtFormatMgr.IPFormat);
			//txtField.filters = filterArrayIP;
			
			newNode.y=-35;
			
			if(color=="red"){
				/*if(_koth){
					newNode.x=40;
				}
				else
				*/newNode.x=20;
			}
			else if(color == "blue"){
				/*if(_koth){
					newNode.x=1024 - newNode.width - 25;
				}
				*/newNode.x=1024 - newNode.width - 5;
			}
			else if(color == "white")
			{
				/*if(_koth){
				newNode.x=-35;
				newNode.y=650;
				}*/
				newNode.x=-55;
				newNode.y=650;
			}
			if (OSType=="KOTH")
			{
				if(color=="red"){
				/*if(_koth){
					newNode.x=40;
				}
				else
				*/
				newNode.x=110;
				newNode.y=6;
			}
			else if(color == "blue"){
				/*if(_koth){
					newNode.x=1024 - newNode.width - 25;
				}
				*/newNode.x=860;
				newNode.y=6;
			}
			else if(color == "white")
			{
				/*if(_koth){
				newNode.x=-35;
				newNode.y=650;
				}*/
				newNode.x=100;
				newNode.y=610;
			}
			}
			
			newNode.teamColor=color;
			
			newNode.alpha=1;
			//SlideRedNodes=20;
			
			newNode.addChild(txtField);
			myScreen.addChild(newNode);
			
			//Add To Idle Array
			if(color=="red")
			{
				RedTeamNodesList.splice(0,0,newNode);
				//RedTeamNodes.splice(0,0,newNode);
			}
			else if(color=="blue")
			{
				BlueTeamNodesList.splice(0,0,newNode);
				//BlueTeamNodes.splice(0,0,newNode);
			}
			else if(color=="white")
			{
				WhiteTeamNodesList.splice(0,0,newNode);
				//WhiteTeamNodes.splice(0,0,newNode);
			}

			//sortNodes();
			return newNode;
		}

		public function duplicateNode(originalNode)
		{
			//Graphic For TextBlock
			//This is for creating a duplicate node for attacking
			var newNode = new MovieClip();
			var newNodeBG:MovieClip;
			if(originalNode.teamColor=="red")
			{
				/*if(originalNode.server)
					newNodeBG = new IPBlockRed();
				else*/
					newNodeBG = new IPBlockRedMini();
			}
			else if(originalNode.teamColor=="blue")
			{
				/*if(originalNode.server)
					newNodeBG = new IPBlockBlue();
				else*/
					newNodeBG = new IPBlockBlueMini();
			}
			else if(originalNode.teamColor=="white")
			{
				/*if(originalNode.server)
					newNodeBG = new IPBlockWhite();
				else*/
					newNodeBG = new IPBlockWhiteMini();
			}
			else
				return;
				
				
			if(!originalNode.server)
			{ 
				
			}
			newNode.osType =originalNode.osType;
			
			newNode.teamColor = originalNode.teamColor;
				
			newNode.numServices=originalNode.numServices;
			
			newNode.server=originalNode.server;
			
			newNodeBG.name="BG";
			newNode.addChild(newNodeBG);
			addOSIcon(newNode);
			var txtField=new TextField();
			/*if(newNode.server)
				txtField.x=2;
			else*/
			txtField.x=20;
			txtField.y=2;
			txtField.text="";
			txtField.name="ip";
			txtField.text=originalNode.getChildByName("ip").text;
			txtField.setTextFormat(TxtFormatMgr.IPFormat);
			
			newNode.x=originalNode.x;
				
				
			newNode.y=originalNode.y;
			
			newNode.alpha=1;
			//SlideRedNodes=20;
			newNode.addChild(txtField);
			myScreen.addChild(newNode);
			return newNode;
			
		}
		
		public function modifyServiceIcon(node,service:String,sstatus:String)
		{
			var tempNode:MovieClip = node;
			var tempNodeIcon = tempNode.getChildByName(service);
			if(tempNodeIcon!=null)
			{		
				tempNodeIcon.alpha = 1;
				if(sstatus=="DOWN")
				{
					tempNodeIcon.gotoAndStop(2);
				}
				if(sstatus=="UP")
				{
					tempNodeIcon.gotoAndStop(1);
				}
			}
		}
		
		public function addOSIcon(node)
		{
			var icon_OS = new Icon_OS();
			icon_OS.name = "os";
			icon_OS.x=4;
			icon_OS.y=5;
			if(node.osType=="windows")
			{
				icon_OS.gotoAndStop(1);
			}
			else if(node.osType=="linux")
			{
				icon_OS.gotoAndStop(3);
			}
			else if(node.osType=="KOTH")
			{
				icon_OS.gotoAndStop(4);
			}
			else
			{
				icon_OS.gotoAndStop(2);
			}
			node.addChild(icon_OS);
		}
		
		public function setOSIcon(node)
		{
			if(node.osType=="windows")
			{
				node.getChildByName("os").gotoAndStop(1);
			}
			else if(node.osType=="linux")
			{
				node.getChildByName("os").gotoAndStop(3);
			}
			else if(node.osType="KOTH")
			{
				node.getChildByName("os").gotoAndStop(4);
			}
			else
			{
				node.getChildByName("os").gotoAndStop(2);
			}
		}
		
		public function removeNodeByObj2(in_node,searchVector,deleted:Boolean, dest:String)
		{
			
			var tempVector:Vector.<MovieClip> = searchVector;
			var i:int = 0;
			trace("removing node by obj- " + in_node);
			for (i=0; i < tempVector.length; i++)
			{
				var tempBlock = tempVector[i];
				if(tempBlock==in_node)
				{
					if(deleted)
					{
						if (dest == "red")
						{
							TweenLite.to(tempBlock,3,{x:(110), y:(6), alpha:0});
							//Tweenlite.to(tempBlock,2,{alpha:0, delay:2});
						}
						else if (dest == "blue")
						{
							TweenLite.to(tempBlock,3,{x:(860), y:6, alpha:0});
							//Tweenlite.to(tempBlock,2,{alpha:0, delay:2});
						}
						else if (dest == "white")
						{
							TweenLite.to(tempBlock,3,{x:(6), y: 610, alpha:0});
							//Tweenlite.to(tempBlock,2,{alpha:0, delay:2});							
						}
					}
					searchVector.splice(i,1);
				}
			}
			//sortNodes();
		}
		public function removeNode(in_name,searchVector,deleted:Boolean)
		{
			
			var tempVector:Vector.<MovieClip> = searchVector;
			var i:int = 0;
			trace("removing node - "+in_name);
			for (i=0; i < tempVector.length; i++)
			{
				var tempBlock = tempVector[i];
				var tempTxt = tempBlock.getChildByName("ip");
				trace("temptxt.text = " + tempTxt.text);
				trace("in name = " + in_name);
				if(tempTxt.text==in_name)
				{
					if(deleted)
						TweenLite.to(tempBlock,1,{x:(-150)});
					tempVector.splice(i,1);
					i=RedTeamNodesList.length;
				}
			}
			//sortNodes();
		}
		
		public function removeNodeByObj(in_node,searchVector,deleted:Boolean)
		{
			
			var tempVector:Vector.<MovieClip> = searchVector;
			var i:int = 0;
			trace("removing node by obj- " + in_node);
			for (i=0; i < tempVector.length; i++)
			{
				var tempBlock = tempVector[i];
				if(tempBlock==in_node)
				{
					if(deleted)
					{
						TweenLite.to(tempBlock,1,{x:(-150)});
						
					}
					searchVector.splice(i,1);
				}
			}
			//sortNodes();
		}
		
		public function findNode(ip,team)
		{
			if(team=="red")
			{
				var i;
				for(i=0;i<RedTeamNodesList.length;i++)
				{
					var tempBlock:MovieClip = RedTeamNodesList[i];
					var nodeIP = tempBlock.getChildByName("ip");
					if(nodeIP.text == ip)
					{
						trace (nodeIP.text + "==" + ip);
						return tempBlock;
					}
				}
			}
			if(team=="blue")
			{
				var i;
				for(i=0;i<BlueTeamNodesList.length;i++)
				{
					var tempBlock:MovieClip = BlueTeamNodesList[i];
					var nodeIP = tempBlock.getChildByName("ip");
					if(nodeIP.text == ip)
					{
						trace (nodeIP.text + "==" + ip);
						return tempBlock;
					}
				}
			}
			if(team=="white")
			{
				var i;
				for(i=0;i<WhiteTeamNodesList.length;i++)
				{
					var tempBlock:MovieClip = WhiteTeamNodesList[i];
					var nodeIP = tempBlock.getChildByName("ip");
					if(nodeIP.text == ip)
					{
						trace (nodeIP.text + "==" + ip);
						return tempBlock;
					}
				}
			}
			return false;
		}
		
		public function findKothNode(ip,team)
		{
			if(team=="red")
			{
				var i;
				for(i=0;i<knr.length;i++)
				{
					var tempBlock:MovieClip = knr[i];
					var nodeIP = tempBlock.getChildByName("ip");
					if(nodeIP.text == ip)
					{
						trace (nodeIP.text + "==" + ip);
						return tempBlock;
					}
				}
			}
			if(team=="blue")
			{
				var i;
				for(i=0;i<knb.length;i++)
				{
					var tempBlock:MovieClip = knb[i];
					var nodeIP = tempBlock.getChildByName("ip");
					if(nodeIP.text == ip)
					{
						trace (nodeIP.text + "==" + ip);
						return tempBlock;
					}
				}
			}
			if(team=="white")
			{
				var i;
				for(i=0;i<knw.length;i++)
				{
					var tempBlock:MovieClip = knw[i];
					var nodeIP = tempBlock.getChildByName("ip");
					if(nodeIP.text == ip)
					{
						trace (nodeIP.text + "==" + ip);
						return tempBlock;
					}
				}
			}
			return false;
		}
	}
	
}